package ru.spbu.math.android.cbrf.dataprovider.cbrf;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.content.res.Resources;

import ru.spbu.math.android.cbrf.model.Category;
import ru.spbu.math.android.cbrf.model.Rate;
import ru.spbu.math.android.cbrf.model.RateType;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Class for parsing data from CBRF and put it in the List of Rate.
 * This class extends DefaultHandler
 * @author  Svetlana Zemlyanskaya
 */

//<Valute ID="R01010">
//	<NumCode>036</NumCode> 
//	<CharCode>AUD</CharCode> 
//	<Nominal>1</Nominal> 
//	<Name>Australian Dollar</Name> 
//	<Value>20,9568</Value> 
//</Valute>

public class CurrencySaxHandler extends DefaultHandler {


    private final String CHAR_CODE = "charCode";
    private final String VALUE = "value";
    private final String VALUTE = "valute";

    private Resources resources;
    private List<Rate> currList;
    
    private Rate currency;
    private StringBuilder builder;

    private Date rateDate;
    
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        builder.append(ch, start, length);
    }

    @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {
        if (this.currency != null) {
            if (localName.equalsIgnoreCase(CHAR_CODE)) {
                String argument = builder.toString().trim();
                RateTypeStore store = RateTypeStore.getInstance(resources);
                List<Category> categories = (List<Category>) store.getCategories();
                RateType type = null;
                for(Category category : categories){
                	List<RateType> rateTypes = (List<RateType>) store.getRateTypesByCategory(category);
                	for(RateType rateType : rateTypes){
                		if(argument.equals(rateType.getSource().getCode())){
                			type = new RateType();
                			type = rateType;
                		}
                	}
                }
                currency.setResource(type);
            } else if (localName.equalsIgnoreCase(VALUE)) {
            	String str = builder.toString().replace(",", ".").trim();
                currency.setValue(Double.parseDouble(str));
            } else if (localName.equalsIgnoreCase(VALUTE)) {
                if (currency.getSource() != null) {
                    currList.add(currency);
                }
            }

            builder.setLength(0);
        }
    }

    @Override
    public void startDocument() throws SAXException {
        currList = new ArrayList<Rate>();
        builder = new StringBuilder();
    }

    @Override
    public void startElement(String uri, String localName, String name,
                             Attributes attributes) throws SAXException {
        if (localName.equalsIgnoreCase(VALUTE)) {
            this.currency = new Rate();
            currency.setDate(rateDate);
        }
    }

    /**
	 * @return rate list from XML
	 */
    public List<Rate> getCurrency() {
        return currList;
    }

    /**
     * Set date for Rate
     *
     * @param date
     */
    public void setDate(Date date) {
        rateDate = date;
    }
    
    /**
     * Set resources to the RateTypeStore
     * @param res
     */
    public void setResources(Resources res) {
    	resources = res;
    }
}
